#include <stdio.h>
#include "HashMap.h"

#define Put(key, value)             HashMap_Put(hashMap, key, value)
#define Get(key)                    HashMap_Get(hashMap, key)
#define GetKesy(keys, count)       HashMap_GetKeys(hashMap, keys, count)
#define Remove(key)                 HashMap_Remove(hashMap, key)
#define Clear()                     HashMap_Clear(hashMap)
#define Exists(key)                 HashMap_Exists(hashMap, key)

int main()
{
    char pairs[][2][10] =
    {
        {"key1", "value1"},
        {"key2", "value2"},
        {"key3", "value3"}
    };
    const unsigned int npair = sizeof(pairs) / sizeof(pairs[0]);

    // 初始化自建内存池
    mem_init();

    // 创建HashMap
    HashMap *hashMap = HashMap_Create(10);

    // 加入和获取
    int i = 0;
    for (i = 0; i != npair; i++)
    {
        Put(pairs[i][0], pairs[i][1]);
        printf("[%s] ", Get(pairs[i][0]));
    }
    printf("\r\n");

    // 查看内存池信息
    printf("memory: <preused:%.2f%%> <used:%d> <free:%d> \r\n", mem_perused(), mem_getused(), mem_getfree());

    // 获取所有key
    int count = 0;
    uint8_t *keys[10];
    GetKesy(keys, &count);
    int j = 0;
    for (j = 0; j < count; ++j)
    {
        printf("[%s] ", keys[j]);
    }
    printf("\r\n");

    // 删除
    Remove("key1");

    // 检测是否存在
    bool isExists = Exists("key1");
    printf("%s\r\n", isExists ? "true" : "false");

    // 清空
    Clear();

    // 销毁HashMap
    HashMap_Destroy(hashMap);

    // 查看内存池信息
    printf("memory: <preused:%.2f%%> <used:%d> <free:%d> \r\n", mem_perused(), mem_getused(), mem_getfree());

    return 0;
}
